package com.operation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mk.HibernateException;
import org.mk.type.MySQLTypeFactory;
import org.mk.type.Type;

import com.db.ColumnInfoBean;
import com.db.ForeignKey;
import com.db.connection.DBCPPostgresManager;
import com.db.sql.convert.DatabaseConverter;
import com.excel.read.Config;
import com.excel.read.DBTable;
import com.mk.SQLTree;
import com.operation.model.OperationModel;
import com.service.ImportService;
import com.xml.Record;
import com.xml.SQLparam;

/**
 * 本地SQL数据操作镜像
 * 
 * @author Administrator
 * 
 */
public class SQLOperationMap extends OperationModel {

	/** 待操作的数据库表名 */
	public String opTableName;
	public int opType = 1;
	public String sql;
	public List<SQLparam> param = new ArrayList<SQLparam>();

	
	
	public SQLOperationMap(long storeFrom) {
		super(storeFrom);
	}

	public int getOperationType() {
		if (sql.indexOf("insert") == 0 || sql.indexOf("INSERT") == 0) {
			opType = ImportService.INSERT;
		} else if (sql.indexOf("delete") == 0 || sql.indexOf("DELETE") == 0) {
			opType = ImportService.INSERT;
		}
		if (sql.indexOf("update") == 0 || sql.indexOf("UPDATE") == 0) {
			opType = ImportService.INSERT;
		}
		return opType;
	}

	public String getSql() {
		return sql;
	}

	public String getType() {
		String type = "";

		return type;
	}

	public void setSql(String sql) {
		this.sql = sql;
		if (sql.indexOf("insert") == 0 || sql.indexOf("INSERT") == 0) {
			opType = ImportService.INSERT;
		} else if (sql.indexOf("delete") == 0 || sql.indexOf("DELETE") == 0) {
			opType = ImportService.DELETE;
		}
		if (sql.indexOf("update") == 0 || sql.indexOf("UPDATE") == 0) {
			opType = ImportService.UPDATE;
		}

	}

	public OperationMap convertToOperationMap() {
		OperationMap aOperationMap = new OperationMap(this.storeFrom);
		Map aMap = null;
		switch (opType) {
		case ImportService.INSERT:
			SQLTree aSQLTree = new SQLTree(sql);
			String[] as = aSQLTree.parse();
			opTableName = as[2];
			aMap = SQLTree.tempInsertMethod(as);
			Record record = new Record();
			record.fieldMap = aMap;
			record.setTableName(opTableName);
			record.op = "insert";
			record.storeFrom = this.storeFrom;
			aOperationMap.record = record;
			aOperationMap.opTableName = opTableName;
			break;
		case ImportService.DELETE:
			break;
		case ImportService.UPDATE:
			break;
		}
		return aOperationMap;
	}

	private void insertOrUpdate(Connection conn) throws HibernateException,
			SQLException {
		String sql = getSql();
		PreparedStatement pstmt = null;
		try {
			// 执行SQL语句
			pstmt = conn.prepareStatement(sql);
			int i = 0;
			for (i = 0; i < param.size(); i++) {
				SQLparam aSQLparam = param.get(i);
				Type type = MySQLTypeFactory.basic(aSQLparam.getType());
				// 判断是否外键
				Object value = null;
				String valueStr = aSQLparam.getObj() == null ? null : aSQLparam
						.getObj().toString();
				Object fieldValue = type.getValue(valueStr);
				if (value != null && value.equals("false")) {
					value = "N";
				} else if (value != null && value.equals("true")) {
					value = "Y";
				}
				type.nullSafeSet(pstmt, fieldValue, i + 1);
			}
			pstmt.executeUpdate();
		} finally {
			DBCPPostgresManager.close(pstmt, null);
		}

	}

	public void insert(Connection conn) throws HibernateException, SQLException {
		insertOrUpdate(conn);
	}

	public void update(Connection conn) throws HibernateException, SQLException {
		insertOrUpdate(conn);
	}

	public void execute(Connection conn) throws SQLException {

	}
	
	
	

}
